import requests, json
import pandas as pd
from PIL import Image
from io import BytesIO

h_key = "9283a3ae76c2742c8a55d9d6e6c75a24"

def weatherInfo(h_key,city,extensions='base'):
    url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params={
        "key":h_key,
        "city":city,
        "extensions":extensions,
        "output":'json'
    }
    response=requests.get(url,params=params)
    results=response.json()['lives'][0]['weather']
    # results=[]
    # for k,v in response.json()['lives'][0].items():
    #     results.append(k+':'+v)
    return results


def geocode(h_key,address,city=None,batch=None,sig=None):
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': h_key,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig,
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    return results

def around(h_key,location,keywords=None,types=None,city=None,radius=None,sortrule=None,offset=None,page=None,extensions='base',sig=None):
    url="https://restapi.amap.com/v3/place/around?parameters"
    params={
        "key":h_key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "radius":radius,
        "sortrule":sortrule,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":'json'
    }
    r=requests.get(url,params=params)
    results=r.json()
    result = ', '.join(pd.json_normalize(results['pois'])['name'].tolist())
    return result

def staticmap(h_key,location,zoom=[1,17],size='200*200',scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    url="https://restapi.amap.com/v3/staticmap?parameters"
    params={
        "key":h_key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic,
        "sig":sig,
        "output":'json'
    }
    response = requests.get(url,params=params)
    data=Image.open(BytesIO(response.content))
    return data